﻿Maiks_GT = LibStub("AceAddon-3.0"):NewAddon("Maiks_GT", "AceConsole-3.0", "AceEvent-3.0", "AceSerializer-3.0", "AceComm-3.0" );

function Maiks_GT:OnInitialize()
    self.db = LibStub("AceDB-3.0"):New("Maiks_GTdb", defaults, "Default")
    LibStub("AceConfig-3.0"):RegisterOptionsTable("Maiks_GT", Maiks_GT:createoptions())
    self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Maiks_GT", "Maiks: GT", "Maiks")
	Maiks_GT:RegisterComm("MAIKSGTUPDATE")
	Maiks_GT:RegisterComm("MAIKSGTREQUEST")
	Maiks_GT:RegisterComm("MAIKSGTDATA")

	if Maiks_GTdb.Teams == nil then
		Maiks_GTdb.Teams = {}
		for i=1,5 do
			Maiks_GTdb.Teams["Team"..i] = {
				Damager1 = "", 
				Damager2 = "", 
				Damager3 = "", 
				Damager4 = "", 
				Damager5 = "", 
				Healer1 = "", 
				Healer2 = "", 
				Healer3 = "", 
				MainTank = "",
				OffTank = "",
			}
		end
	end

	if Maiks_GTdb.TeamsReplace == nil then
		Maiks_GTdb.TeamsReplace = {}
		for i=1,5 do
			Maiks_GTdb.TeamsReplace["Team"..i] = {
				Damager1 = "", 
				Damager2 = "", 
				Damager3 = "", 
				Damager4 = "", 
				Damager5 = "", 
				Healer1 = "", 
				Healer2 = "", 
				Healer3 = "", 
				MainTank = "",
				OffTank = "",
			}
		end
	end
	if Maiks_GTdb.EditRank == nil then Maiks_GTdb.EditRank = 1 end
end

function Maiks_GT:OnEnable()
    -- Called when the addon is enabled
end

function Maiks_GT:OnDisable()
    -- Called when the addon is disabled
end

function Maiks_GT:createoptions()

	options = {
		name = "Maiks: GT",
		handler = Maiks_GT,
		type = "group",
		childGroups = "tab",
		args = {
			Team1 = {
				name = "Team 1",
				type = "group",
				order = 1,
				args = {
					MainTank = {
						name = "Main Tank",
						desc = "",
						type = "select",
						order = 2,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Tank") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "MainTank", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "MainTank") end,
					},
					MainTankReplace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 2.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "MainTank", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "MainTank") end,
					},
					OffTank = {
						name = "Off Tank",
						desc = "",
						type = "select",
						order = 3,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Tank") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "OffTank", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "OffTank") end,
					},
					OffTankReplace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 3.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "OffTank", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "OffTank") end,
					},
					Healer1 = {
						name = "Healer 1",
						desc = "",
						type = "select",
						order = 4,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Healer") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Healer1", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Healer1") end,
					},
					Healer1Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 4.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Healer1", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Healer1") end,
					},
					Healer2 = {
						name = "Healer 2",
						desc = "",
						type = "select",
						order = 5,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Healer") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Healer2", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Healer2") end,
					},
					Healer2Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 5.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Healer2", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Healer2") end,
					},
					Healer3 = {
						name = "Healer 3",
						desc = "",
						type = "select",
						order = 6,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Healer") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Healer3", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Healer3") end,
					},
					Healer3Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 6.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Healer3", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Healer3") end,
					},
					Damager1 = {
						name = "Damager 1",
						desc = "",
						type = "select",
						order = 7,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Damager") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Damager1", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Damager1") end,
					},
					Damager1Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 7.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Damager1", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Damager1") end,
					},
					Damager2 = {
						name = "Damager 2",
						desc = "",
						type = "select",
						order = 8,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Damager") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Damager2", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Damager2") end,
					},
					Damager2Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 8.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Damager2", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Damager2") end,
					},
					Damager3 = {
						name = "Damager 3",
						desc = "",
						type = "select",
						order = 9,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Damager") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Damager3", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Damager3") end,
					},
					Damager3Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 9.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Damager3", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Damager3") end,
					},
					Damager4 = {
						name = "Damager 4",
						desc = "",
						type = "select",
						order = 10,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Damager") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Damager4", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Damager4") end,
					},
					Damager4Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 10.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Damager4", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Damager4") end,
					},
					Damager5 = {
						name = "Damager 5",
						desc = "",
						type = "select",
						order = 11,
						style = "dropdown",
						values = function() return Maiks_GT:GuildMembers("Damager") end,
						set = function(info, arg1) Maiks_GT:GuildTeams("set", "Team1", "Damager5", arg1) end,
						get = function(info) return Maiks_GT:GuildTeams("get", "Team1", "Damager5") end,
					},
					Damager5Replace = {
						name = "Replace?",
						desc = "",
						type = "toggle",
						order = 11.1,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						tristate = false,
						set = function(info, arg1) Maiks_GT:GuildTeamsReplace("set", "Team1", "Damager5", arg1) end,
						get = function(info) return Maiks_GT:GuildTeamsReplace("get", "Team1", "Damager5") end,
					},
				},
			},      
			options = {
				name = "Options",
				type = "group",
				order = 100,
				args = {
					MainTank = {
						name = "What rank is allowed to change settings?",
						desc = "",
						type = "select",
						order = 2,
						disabled = function() return Maiks_GT:GuildRanks("disable") end,
						width = "double",
						style = "dropdown",
						values = function() return Maiks_GT:GuildRanks("val") end,
						set = function(info, arg1) Maiks_GT:GuildRanks("set", arg1) end,
						get = function(info) return Maiks_GT:GuildRanks("get") end,
					},
				},
			},
		},
	}

	return options
end

function Maiks_GT:GuildMembers(arg1)
	local GuildMembers = {Damager = {}, Healer = {}, Tank = {}}
	for i=1,GetNumGuildMembers() do
		local name,_,_,level,class,_,_,_,_,_,_,_,_,_ = GetGuildRosterInfo(i)
		if level == 85 then 
			if class == "Death Knight" then
				GuildMembers.Damager[name] = name 
				GuildMembers.Tank[name] = name
			elseif class == "Druid" then
				GuildMembers.Damager[name] = name 
				GuildMembers.Healer[name] = name 
				GuildMembers.Tank[name] = name
			elseif class == "Hunter" then
				GuildMembers.Damager[name] = name 
			elseif class == "Mage" then
				GuildMembers.Damager[name] = name 
			elseif class == "Paladin" then
				GuildMembers.Damager[name] = name 
				GuildMembers.Healer[name] = name 
				GuildMembers.Tank[name] = name
			elseif class == "Priest" then
				GuildMembers.Damager[name] = name 
				GuildMembers.Healer[name] = name 
			elseif class == "Rogue" then
				GuildMembers.Damager[name] = name 
			elseif class == "Shaman" then
				GuildMembers.Damager[name] = name 
				GuildMembers.Healer[name] = name 
			elseif class == "Warlock" then
				GuildMembers.Damager[name] = name 
			elseif class == "Warrior" then
				GuildMembers.Damager[name] = name 
				GuildMembers.Tank[name] = name
			end
		end
		GuildMembers.Damager["None"] = "None" 
		GuildMembers.Healer["None"] = "None" 
		GuildMembers.Tank["None"] = "None"
	end
	if arg1 == "Damager" then
		return GuildMembers.Damager
	elseif arg1 == "Healer" then
		return GuildMembers.Healer
	elseif arg1 == "Tank" then
		return GuildMembers.Tank
	end
end
	
function Maiks_GT:GuildTeams(arg1, arg2, arg3, arg4)
	if arg1 == "get" then
		if Maiks_GTdb.Teams[arg2][arg3] == "" then
			return "None"
		else
			return Maiks_GTdb.Teams[arg2][arg3]
		end
	elseif arg1 == "set" then
		Maiks_GTdb.Teams[arg2][arg3] = arg4
		Maiks_GT:Update()
	end
end
	
function Maiks_GT:GuildTeamsReplace(arg1, arg2, arg3, arg4)
	if arg1 == "get" then
		if Maiks_GTdb.TeamsReplace[arg2][arg3] == "" then
			return false
		else
			return Maiks_GTdb.TeamsReplace[arg2][arg3]
		end
	elseif arg1 == "set" then
		Maiks_GTdb.TeamsReplace[arg2][arg3] = arg4
		Maiks_GT:Update()
	end
end

function Maiks_GT:GuildRanks(arg1, arg2)
	if arg1 == "val" then
		local guildrank = {}
		for i=1,GuildControlGetNumRanks() do
			local rankName = GuildControlGetRankName(i)
			guildrank[i] = rankName
		end
		return guildrank
	elseif arg1 == "get" then
		if Maiks_GTdb.EditRank == nil then
			return "[1]"
		else
			return Maiks_GTdb.EditRank
		end
	elseif arg1 == "set" then
		Maiks_GTdb.EditRank = arg2
		Maiks_GT:Update()
	elseif arg1 == "disable" then
		
		local guildName, guildRankName, guildRankIndex = GetGuildInfo("player")
		if guildRankIndex + 1 <= Maiks_GTdb.EditRank then
			return false
		else
			return true
		end
	end
end

function Maiks_GT:Update()
	local hours, minutes = GetGameTime()
	local weekday, month, day, year = CalendarGetDate()
	if strlen(month) == 1 then month = "0"..month end
	if strlen(day) == 1 then month = "0"..day end
	if strlen(hours) == 1 then hours = "0"..month end
	if strlen(minutes) == 1 then minutes = "0"..month end
	Maiks_GTdb.Update = year..month..day..hours..minutes
end

function Maiks_GT:OnCommReceived(prefix, text, distribution, target, prio)
	if prefix == "MAIKSGTUPDATE" then 
		if Maiks_GTdb.Update == text then
			return
		elseif Maiks_GTdb.Update >= text then
			Maiks_GT:SendCommMessage("MAIKSGTUPDATE", Maiks_GTdb.Update, "GUILD", nil, "ALERT")
		elseif Maiks_GTdb.Update <= text then
			Maiks_GT:SendCommMessage("MAIKSGTREQUEST", Maiks_GTdb.Update, "GUILD", nil, "ALERT")
		end
	elseif prefix == "MAIKSGTREQUEST" then
		Serialized = Maiks_GT:Serialize(Maiks_GTdb.Teams, Maiks_GTdb.TeamsReplace, Maiks_GTdb.EditRank, Maiks_GTdb.Update)
		Maiks_GT:SendCommMessage("MAIKSGTDATA", Serialized, "GUILD", nil, "BULK")
	elseif prefix == "MAIKSGTDATA" then
		Maiks_GTdb.Teams, Maiks_GTdb.TeamsReplace, Maiks_GTdb.EditRank, Maiks_GTdb.Update = Maiks_GT:Deserialize(text)
	end
		
end

